﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.Pop.usControl
{
    public partial class PotentialCustomerCase : System.Web.UI.UserControl, System.Web.UI.IPostBackEventHandler
    {
        /// <summary>
        /// Gets or sets the potential customer case status.
        /// </summary>
        /// <value>
        /// The potential customer case status.
        /// </value>
        public string PotentialCustomerCaseStatus
        {
            get { return (ViewState["PotentialCustomerCaseStatus"] == null) ? "" : ViewState["PotentialCustomerCaseStatus"].ToString(); }
            set { ViewState["PotentialCustomerCaseStatus"] = value; }
        }
        /// <summary>
        /// Gets or sets the potential customer case ID.
        /// </summary>
        /// <value>
        /// The potential customer case ID.
        /// </value>
        public string PotentialCustomerCaseID
        {
            get { return (ViewState["PotentialCustomerCaseID"] == null) ? "" : ViewState["PotentialCustomerCaseID"].ToString(); }
            set { ViewState["PotentialCustomerCaseID"] = value; }
        }
        /// <summary>
        /// 客户信息 Place Holder 容器
        /// Gets or sets the place holder customer info.
        /// </summary>
        /// <value>
        /// The place holder customer info.
        /// </value>
        public PlaceHolder PlaceHolderCustomerInfo { get; set; }
        /// <summary>
        /// 父页面
        /// Gets or sets the parent page.
        /// </summary>
        /// <value>
        /// The parent page.
        /// </value>
        public Page ParentPage { get; set; }
        /// <summary>
        /// 用户登录信息
        /// Gets or sets the user login info.
        /// </summary>
        /// <value>
        /// The user login info.
        /// </value>
        public UserLoginInfo UserLoginInfo { get; set; }
        /// <summary>
        /// Gets or sets the sale vehicle entity.
        /// </summary>
        /// <value>
        /// The sale vehicle entity.
        /// </value>
        public T_SALEVEHICLE_Entity SaleVehicleEntity { get; set; }


        #region 对象访问业务类

        /// <summary>
        ///  客户信息
        /// </summary>
        T_CUSTOMER_BLL customerBll = new T_CUSTOMER_BLL();
        /// <summary>
        /// 
        /// </summary>
        T_POTENTIALCUSTOMERCASE_BLL potentialCustomerCaseBll = new T_POTENTIALCUSTOMERCASE_BLL();
        /// <summary>
        /// 数据字典
        /// </summary>
        T_DICTIONARY_BLL dictionaryBll = new T_DICTIONARY_BLL();
        /// <summary>
        /// 工单状态
        /// </summary>
        private T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();
        /// <summary>
        /// 市场部
        /// </summary>
        T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        /// <summary>
        /// 经销商
        /// </summary>
        T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        /// <summary>
        /// 
        /// </summary>
        private T_WORKORDEROPERATION_BLL workOrderOperationBll = new T_WORKORDEROPERATION_BLL();
        /// <summary>
        /// 配置表
        /// </summary>
        private T_CONFIG_BLL configBll = new T_CONFIG_BLL();
        /// <summary>
        /// 工单操作日志
        /// </summary>
        private T_WORKORDERLOG_BLL workOrderLogBll = new T_WORKORDERLOG_BLL();
        /// <summary>
        /// 录音
        /// </summary>
        private T_RECORD_BLL recordBll = new T_RECORD_BLL();

        /// <summary>
        /// 
        /// </summary>
        T_CUSTOMERTELINDEX_BLL ctBll = new T_CUSTOMERTELINDEX_BLL();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_Page_LoadBegin");
            if (!IsPostBack)
            {
                ddlREMINDERTIMEBinding();
                ddlCONSULTINGTYPEBinding();
                ddlCUSTOMERSTATUSBinding();

                ddlVEHICLEMARKCODEBinding();
                ddlDEALERCODEBinding();
                try
                {
                    //ddlREMINDERTIME.SelectedValue = configBll.GetCacheValueByName(this.Page, "ConsultingDefaultReminderTime");
                }
                catch (Exception ex)
                {
                }
                pageInit();


            }

            LoadButton();
            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_Page_LoadEnd");
        }


        /// <summary>
        /// 当由类实现时，使服务器控件能够处理将窗体发送到服务器时引发的事件。
        /// </summary>
        /// <param name="eventArgument">表示要传递到事件处理程序的可选事件参数的 <see cref="T:System.String"/>。</param>
        public void RaisePostBackEvent(string eventArgument)
        {
            Button btn = phButton.FindControl(eventArgument) as Button;
            if (btn != null)
            {
                btnOK_Click(btn, null);
            }
        }

        /// <summary>
        /// 
        /// Loads the button.
        /// </summary>
        public void LoadButton()
        {
            phButton.Controls.Clear();
            if (String.IsNullOrEmpty(lblPotentialCustomerCaseID.Text.Trim()))
            {
                Button btn = new Button();
                btn.ID = StaticConstant.PrePotentialCustomerCase + "SAVE";
                btn.CommandName = StaticConstant.PrePotentialCustomerCase + "SAVE";
                btn.Text = "保 存";
                btn.CssClass = "btn";
                //if (entity.ISCONFIRM.ToString() == ((int)IsConfirm.是).ToString())
                //{
                //    btn.OnClientClick = String.Format("return confirm('您确认要执行 {0} 操作吗？');", entity.OPERATIONNAME);
                //}
                //btn.Click += new EventHandler(btnOK_Click);
                StringBuilder sb = new StringBuilder();
                sb.Append("if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }};");
                sb.Append("this.disabled  = true;");
                sb.Append(this.Page.ClientScript.GetPostBackEventReference(this, btn.ID.ToString()));
                sb.Append(";");
                btn.Attributes.Add("onclick", sb.ToString());
                ////btn.OnClientClick = "this.disabled=true;this.form.submit();";
                //btn.UseSubmitBehavior = false;
                phButton.Controls.Add(btn);
            }
            else
            {
                string conditions = " and CurrentStatusID = '{0}' and OrderTypeID = '{1}' and IsPopPage = '{2}' ";
                string orders = " SortOrder desc, OperationID asc ";                
                List<T_WORKORDEROPERATION_Entity> workOrderOperationList = workOrderOperationBll.GetList(String.Format(conditions, PotentialCustomerCaseStatus, ((int)WorkOrderType.潜在客户).ToString(), ((int)IsPopPage.是).ToString()), orders);
                foreach (var entity in workOrderOperationList)
                {
                    Button btn = new Button();
                    btn.ID = StaticConstant.PrePotentialCustomerCase + entity.OPERATIONID.ToString();
                    btn.CommandName = StaticConstant.PrePotentialCustomerCase + entity.OPERATIONID.ToString();
                    btn.Text = entity.OPERATIONNAME;
                    btn.CssClass = "btn";
                    if (entity.ISCONFIRM.ToString() == ((int)IsConfirm.是).ToString())
                    {
                        btn.OnClientClick = String.Format("return confirm('您确认要执行 {0} 操作吗？');", entity.OPERATIONNAME);
                    }
                    btn.Click += new EventHandler(btnOK_Click);
                    phButton.Controls.Add(btn);
                }
            }
        }

        public void pageInit()
        {
            if (String.IsNullOrEmpty(lblPotentialCustomerCaseID.Text.Trim()))
            {
                PotentialCustomerCaseStatus = "";
                if (SaleVehicleEntity != null)
                {
                    txtPRODUCTCODE.Text = SaleVehicleEntity.PRODUCTCODE;
                    txtBRAND.Text = SaleVehicleEntity.BRAND;
                    txtVEHICLETYPE.Text = SaleVehicleEntity.VEHICLETYPE;
                    txtSERIES.Text = SaleVehicleEntity.SERIES;
                    txtMODELS.Text = SaleVehicleEntity.MODELS;
                    txtENGINEMODEL.Text = SaleVehicleEntity.ENGINEMODEL;
                    txtENGINEMANUFACTURERS.Text = SaleVehicleEntity.ENGINEMANUFACTURERS;
                }
                else
                {
                    PageHelper.ClearControlValue(this);
                    try
                    {
                        //ddlREMINDERTIME.SelectedValue = configBll.GetCacheValueByName(this.Page, "PotentialCustomerDefaultReminderTime");
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            else
            {

            }


        }



        #region 页面初始化数据绑定



        /// <summary>
        /// DDLs the CONSULTINGTYPE binding.
        /// </summary>
        private void ddlCONSULTINGTYPEBinding()
        {
            ddlCONSULTINGTYPE.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ConsultingType"));
            ddlCONSULTINGTYPE.DataTextField = "DictValue";
            ddlCONSULTINGTYPE.DataValueField = "DictKey";
            ddlCONSULTINGTYPE.DataBind();
            ddlCONSULTINGTYPE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// 闭环时间
        /// DDLs the REMINDERTIME binding.
        /// </summary>
        private void ddlREMINDERTIMEBinding()
        {

            //ddlREMINDERTIME.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ConsultingReminderTime"));
            //ddlREMINDERTIME.DataTextField = "DictValue";
            //ddlREMINDERTIME.DataValueField = "DictKey";
            //ddlREMINDERTIME.DataBind();
            //ddlREMINDERTIME.Items.Insert(0, new ListItem("无提醒", ""));
        }

        private void ddlCUSTOMERSTATUSBinding()
        {
            ddlCUSTOMERSTATUS.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerStatus"));
            ddlCUSTOMERSTATUS.DataTextField = "DictValue";
            ddlCUSTOMERSTATUS.DataValueField = "DictKey";
            ddlCUSTOMERSTATUS.DataBind();
            ddlCUSTOMERSTATUS.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlVEHICLEMARKCODEBinding()
        {
            string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode), " MarketDepartmentNAME asc ")
                            .Select(p => new
                            {
                                Name = (p.MARKETDEPARTMENTNAME.Length > 0) ? StringHelper.GetChineseSpell(p.MARKETDEPARTMENTNAME)[0].ToString() + "-" + p.MARKETDEPARTMENTNAME : p.MARKETDEPARTMENTNAME,
                                ID = p.MARKETDEPARTMENTID,
                                Code = p.MARKETDEPARTMENTCODE
                            });
            ddlVEHICLEMARKCODE.DataSource = list;
            ddlVEHICLEMARKCODE.DataTextField = "Name";
            ddlVEHICLEMARKCODE.DataValueField = "Code";
            ddlVEHICLEMARKCODE.DataBind();
            ddlVEHICLEMARKCODE.Items.Insert(0, new ListItem("", ""));

        }


        private void ddlDEALERCODEBinding()
        {
            string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            var list = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and  MARKETDEPARTMENTCODE = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ddlVEHICLEMARKCODE.SelectedValue.Trim()), " DEALERNAME asc ")
                            .Select(p => new
                            {
                                DealerName = (p.DEALERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.DEALERNAME)[0].ToString() + "-" + p.DEALERNAME : p.DEALERNAME,
                                DealerID = p.DEALERID,
                                DealerCode = p.DEALERCODE
                            });
            ddlDEALERCODE.DataSource = list;
            ddlDEALERCODE.DataTextField = "DealerName";
            ddlDEALERCODE.DataValueField = "DealerCode";
            ddlDEALERCODE.DataBind();
            ddlDEALERCODE.Items.Insert(0, new ListItem("", ""));
        }


        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string strUpdateContent = "";
            StringBuilder sbTemp = new StringBuilder();
            if (!(sender is Button)) return;
            Button btn = sender as Button;

            if (btn.ID == StaticConstant.PrePotentialCustomerCase + "SAVE") //保存
            {

                #region 工单信息保存

                string customerId = "";

                #region 客户资料更新

                UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_btnOK_Click");
                HiddenField hfCustomerId = this.PlaceHolderCustomerInfo.FindControl("hfCustomerId") as HiddenField;
                if (hfCustomerId == null)
                {
                    PageHelper.ShowMessage(this.Page, "请选择客户！");
                    return;
                }
                customerId = hfCustomerId.Value;
                T_CUSTOMER_Entity customerEntity = customerBll.GetEntity("CustomerId", customerId);
                if (customerEntity == null)
                {
                    PageHelper.ShowMessage(this.Page, "请选择客户！");
                    return;
                }
                PageHelper.GetControlValue<T_CUSTOMER_Entity>(this.PlaceHolderCustomerInfo, ref customerEntity);
                customerBll.Update(customerEntity);
                ctBll.UpdateCustomerTelIndex(customerEntity);

                UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_btnOK_Click_CustomerUpdate");
                #endregion

                T_POTENTIALCUSTOMERCASE_Entity potentialCustomerCaseEntity = new T_POTENTIALCUSTOMERCASE_Entity();
                potentialCustomerCaseEntity.CASEID = potentialCustomerCaseBll.GeneratorCaseID();
                strUpdateContent = PageHelper.GetControlValue<T_POTENTIALCUSTOMERCASE_Entity>(this.PlaceHolderCustomerInfo, ref potentialCustomerCaseEntity);
                strUpdateContent += PageHelper.GetControlValue<T_POTENTIALCUSTOMERCASE_Entity>(this, ref potentialCustomerCaseEntity);
                potentialCustomerCaseEntity.BUSINESSDIVISIONCODE = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                potentialCustomerCaseEntity.BUSINESSDIVISIONID = UserLoginInfo.UserEntity.BUSINESSDIVISIONID;
                potentialCustomerCaseEntity.BUSINESSDIVISIONNAME = UserLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                potentialCustomerCaseEntity.CUSTOMERID = customerId;
                potentialCustomerCaseEntity.CUSTOMERCODE = customerEntity.CUSTOMERCODE;
                potentialCustomerCaseEntity.CALLED = PageHelper.GetQueryString(this.Page, "CALLED");
                potentialCustomerCaseEntity.CALLER = PageHelper.GetQueryString(this.Page, "PHONE");
                potentialCustomerCaseEntity.CALLID = PageHelper.GetQueryString(this.Page, "CALLID");
                potentialCustomerCaseEntity.CALLTYPE = PageHelper.GetQueryString(this.Page, "CALLTYPE");
                potentialCustomerCaseEntity.RECORDCODE = PageHelper.GetQueryString(this.Page, "RECORDCODE");


                potentialCustomerCaseEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                potentialCustomerCaseEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                potentialCustomerCaseEntity.CREATETIME = DateTime.Now;
                potentialCustomerCaseEntity.LASTMODIFYPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                potentialCustomerCaseEntity.LASTMODIFYPEOPLEID = UserLoginInfo.UserEntity.USERID;
                potentialCustomerCaseEntity.LASTMODIFYTIME = DateTime.Now;

                potentialCustomerCaseEntity.CREATEDEPARTMENTID = UserLoginInfo.UserEntity.DEPARTMENTID;
                potentialCustomerCaseEntity.CREATEDEPARTMENTNAME = UserLoginInfo.UserEntity.DEPARTMENTNAME;
                potentialCustomerCaseEntity.CREATEUSERTYPE = UserLoginInfo.UserEntity.USERTYPE;

                //if (StringHelper.IsNumber(ddlREMINDERTIME.SelectedValue.Trim()))
                //{
                //    potentialCustomerCaseEntity.TODOREMINDERTIME = DateTime.Now.AddHours(StringHelper.ToInt(ddlREMINDERTIME.SelectedValue.Trim(), 0));
                //}


                try
                {
                    potentialCustomerCaseEntity.COUNTRY = (this.PlaceHolderCustomerInfo.FindControl("ddlCOUNTRY") as DropDownList).SelectedItem.Text;
                    potentialCustomerCaseEntity.PROVINCE = (this.PlaceHolderCustomerInfo.FindControl("ddlPROVINCE") as DropDownList).SelectedItem.Text;
                    potentialCustomerCaseEntity.PREFECTURECITY = (this.PlaceHolderCustomerInfo.FindControl("ddlPREFECTURECITY") as DropDownList).SelectedItem.Text;
                    potentialCustomerCaseEntity.CITY = (this.PlaceHolderCustomerInfo.FindControl("ddlCITY") as DropDownList).SelectedItem.Text;
                    potentialCustomerCaseEntity.VILLAGETOWN = (this.PlaceHolderCustomerInfo.FindControl("ddlVILLAGETOWN") as DropDownList).SelectedItem.Text;
                }
                catch
                {
                }


                #region 设定工单状态
                potentialCustomerCaseEntity.STATUS = configBll.GetCacheValueByName(this.Page, "PotentialCustomerCaseBeginStatus");
                #endregion

                if (String.IsNullOrEmpty(potentialCustomerCaseEntity.ADDRESS))
                {
                    StringBuilder sbAddress = new StringBuilder();
                    sbAddress.Append(potentialCustomerCaseEntity.COUNTRY).Append("-");
                    sbAddress.Append(potentialCustomerCaseEntity.PROVINCE).Append("-");
                    sbAddress.Append(potentialCustomerCaseEntity.PREFECTURECITY).Append("-");
                    sbAddress.Append(potentialCustomerCaseEntity.CITY).Append("-");
                    sbAddress.Append(potentialCustomerCaseEntity.VILLAGETOWN).Append("-");
                    sbAddress.Append(potentialCustomerCaseEntity.VILLAGE).Append("-");
                    sbAddress.Append(potentialCustomerCaseEntity.GROUPTEAM);
                    potentialCustomerCaseEntity.ADDRESS = sbAddress.ToString();
                }

                potentialCustomerCaseEntity.VEHICLEMARKNAME = marketDepartmentBll.GetMarketDepartmentName(this.Page, potentialCustomerCaseEntity.VEHICLEMARKCODE.Trim(), UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim());

                UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_btnOK_Click_CaseBegin");
                long rel = potentialCustomerCaseBll.Insert(potentialCustomerCaseEntity);

                PotentialCustomerCaseStatus = potentialCustomerCaseEntity.STATUS;
                lblPotentialCustomerCaseID.Text = potentialCustomerCaseEntity.CASEID.ToString();
                if (rel > 0)
                {
                    #region 设置关联车辆

                    UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_btnOK_Click_VehicleRelationBegin");
                    potentialCustomerCaseBll.SetVehicleRelation(potentialCustomerCaseEntity, UserLoginInfo);

                    #endregion

                    #region 录音

                    //RecordWrite(consultingCaseEntity);
                    recordBll.XmlUrl = configBll.GetCacheValueByName(this.Page, "WFOSearch");
                    recordBll.WfoUserName = configBll.GetCacheValueByName(this.Page, "WFOUserName");
                    recordBll.WfoPassword = configBll.GetCacheValueByName(this.Page, "WFOPassword");
                    recordBll.UserLoginInfo = UserLoginInfo;
                    System.Threading.Thread SyncData = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(recordBll.ConsultingCaseRecordWrite));
                    SyncData.Start(potentialCustomerCaseEntity);


                    #endregion

                    #region 状态变更日志

                    if (configBll.GetCacheValueByName(this.Page, "WorkOrderLog").ToLower() != "False")
                    {
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_btnOK_Click_workOrderLogBegin");
                        T_WORKORDERLOG_Entity workOrderLogEntity = new T_WORKORDERLOG_Entity();
                        workOrderLogEntity.CASEID = potentialCustomerCaseEntity.CASEID;
                        workOrderLogEntity.CUSTOMERCODE = potentialCustomerCaseEntity.CUSTOMERCODE;
                        workOrderLogEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                        workOrderLogEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                        workOrderLogEntity.CREATETIME = DateTime.Now;
                        workOrderLogEntity.ORDERTYPEID = (int)WorkOrderType.潜在客户;
                        workOrderLogEntity.ORDERTYPENAME = (WorkOrderType.潜在客户).ToString();
                        workOrderLogEntity.OPERATIONNAME = btn.Text;
                        workOrderLogEntity.UPDATECONTENT = strUpdateContent + sbTemp.ToString();
                        workOrderLogEntity.NEXTSTATUSID = StringHelper.ToInt(potentialCustomerCaseEntity.STATUS, 0);
                        try
                        {
                            workOrderLogEntity.NEXTSTATUSNAME = workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_EntityByKey(this.Page, StringHelper.ToInt(potentialCustomerCaseEntity.STATUS, 0)).ORDERSTATUSNAME;
                        }
                        catch (Exception)
                        {

                        }
                        workOrderLogBll.Insert(workOrderLogEntity);

                    }

                    UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "PotentialCustomerCase_btnOK_Click_End");
                    #endregion

                    LoadButton();
                    PageHelper.ShowMessage(this.Page, "工单保存成功！");
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "工单保存失败！");
                }

                #endregion
            }
            else
            {
                string operationId = btn.CommandName.Replace(StaticConstant.PrePotentialCustomerCase, "");
                T_WORKORDEROPERATION_Entity workOrderOperationEntity = workOrderOperationBll.GetCache_T_WORKORDEROPERATION_EntityByKey(this.Page, StringHelper.ToInt(operationId, 0));

                string _caseid = lblPotentialCustomerCaseID.Text.Trim();
                T_POTENTIALCUSTOMERCASE_Entity entity = potentialCustomerCaseBll.GetEntity("CASEID", _caseid);
                if (entity == null)
                {
                    return;
                }

                if ((IsUpdateInfo)Enum.Parse(typeof(IsUpdateInfo), workOrderOperationEntity.ISUPDATEINFO.ToString()) == IsUpdateInfo.是)
                {
                    strUpdateContent = PageHelper.GetControlValue<T_POTENTIALCUSTOMERCASE_Entity>(this, ref entity);
                }

                // 执行相关反射操作
                MethodResult result = new MethodResult();
                workOrderOperationBll.AssociatedAction<T_POTENTIALCUSTOMERCASE_Entity>(workOrderOperationEntity, entity, result);
                if (result.Result == MethodResult.ResultType.Failure)
                {
                    PageHelper.ShowMessage(this.Page, result.Message);
                    return;
                }

                entity.STATUS = workOrderOperationEntity.NEXTSTATUSID.ToString();

                entity.LASTMODIFYPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = UserLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;

                entity.VEHICLEMARKNAME = marketDepartmentBll.GetMarketDepartmentName(this.Page, entity.VEHICLEMARKCODE.Trim(), UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim());

                long rel = potentialCustomerCaseBll.Update(entity);
                PotentialCustomerCaseStatus = entity.STATUS;
                if (rel > 0)
                {
                    #region 更新到中间表
                    //if (workOrderStatusBll.GetSessionStatusType(this.Page, entity.STATUS) == StatusType.结束节点)
                    //{
                    //    try
                    //    {
                    //        CC_CONSULTINGCASE_BLL ccBll = new CC_CONSULTINGCASE_BLL();
                    //        CC_CONSULTINGCASE_Entity ccEntity = new CC_CONSULTINGCASE_Entity();
                    //        ccBll._dal.ObjectCopyTo(entity, ccEntity);
                    //        ccBll.Delete("CASEID", entity.CASEID.ToString());
                    //        ccBll.Insert(ccEntity);
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        LogHelper.WriteLog("MiddleDataBase", String.Format("CaseID:{0}--Message:{1}", entity.CASEID.ToString(), ex.Message));

                    //    }
                    //}
                    #endregion
                    #region 状态变更日志

                    try
                    {
                        if (configBll.GetCacheValueByName(this.Page, "WorkOrderLog").ToLower() != "False")
                        {
                            T_WORKORDERLOG_Entity workOrderLogEntity = new T_WORKORDERLOG_Entity();
                            workOrderLogBll._dal.ObjectCopyTo(workOrderOperationEntity, workOrderLogEntity);
                            workOrderLogEntity.CASEID = entity.CASEID;
                            workOrderLogEntity.CUSTOMERCODE = entity.CUSTOMERCODE;
                            workOrderLogEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                            workOrderLogEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                            workOrderLogEntity.CREATETIME = DateTime.Now;
                            workOrderLogEntity.UPDATECONTENT = strUpdateContent + sbTemp.ToString();
                            workOrderLogBll.Insert(workOrderLogEntity);
                        }
                    }
                    catch (Exception ex)
                    {
                    }

                    #endregion

                    LoadButton();
                    PageHelper.ShowMessage(this.Page,
                                           String.Format("执行操作 {0} 成功！工单状态由 {1} 更新为 {2} ！", workOrderOperationEntity.OPERATIONNAME, workOrderOperationEntity.CURRENTSTATUSNAME, workOrderOperationEntity.NEXTSTATUSNAME));
                }
                else
                {
                    PageHelper.ShowMessage(this.Page,
                                           String.Format("执行操作 {0} 失败！", workOrderOperationEntity.OPERATIONNAME));
                }
            }
        }

        private void RecordWrite(T_POTENTIALCUSTOMERCASE_Entity potentialCustomerCaseEntity)
        {
            try
            {
                T_RECORD_Entity recordEntity = recordBll.GetRemoteInfo(potentialCustomerCaseEntity.CALLID);
                recordEntity.CALLTYPE = potentialCustomerCaseEntity.CALLTYPE;
                recordEntity.CASEID = potentialCustomerCaseEntity.CASEID;
                recordEntity.CALLER = potentialCustomerCaseEntity.CALLER;
                recordEntity.CALLED = potentialCustomerCaseEntity.CALLED;
                recordEntity.CASETYPE = (int)WorkOrderType.潜在客户;
                recordEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                recordEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                recordEntity.CREATETIME = DateTime.Now;
                recordEntity.BUSINESSDIVISIONCODE = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                recordEntity.BUSINESSDIVISIONID = UserLoginInfo.UserEntity.BUSINESSDIVISIONID;
                recordEntity.BUSINESSDIVISIONNAME = UserLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                recordBll.Insert(recordEntity);
            }
            catch (Exception ex)
            {

            }
        }

        protected void ddlVEHICLEMARKCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDEALERCODEBinding();
        }





    }
}